package com.chinags.system.expert.service.impl;

import com.chinags.common.core.text.Convert;
import com.chinags.common.exception.BusinessException;
import com.chinags.common.utils.StringUtils;
import com.chinags.system.expert.domain.TJcExpertInfo;
import com.chinags.system.expert.mapper.TJcExpertInfoMapper;
import com.chinags.system.expert.service.ITJcExpertInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 专家 服务层实现
 * 
 * @author liut
 * @date 2019-06-20
 */
@Service
public class TJcExpertInfoServiceImpl implements ITJcExpertInfoService
{
	@Autowired
	private TJcExpertInfoMapper tJcExpertInfoMapper;

	private static final Logger log = LoggerFactory.getLogger(TJcExpertInfoServiceImpl.class);
	/**
     * 查询专家信息
     * 
     * @param expertInfoId 专家ID
     * @return 专家信息
     */
    @Override
	public TJcExpertInfo selectTJcExpertInfoById(Long expertInfoId)
	{
	    return tJcExpertInfoMapper.selectTJcExpertInfoById(expertInfoId);
	}
	
	/**
     * 查询专家列表
     * 
     * @param tJcExpertInfo 专家信息
     * @return 专家集合
     */
	@Override
	public List<TJcExpertInfo> selectTJcExpertInfoList(TJcExpertInfo tJcExpertInfo)
	{
	    return tJcExpertInfoMapper.selectTJcExpertInfoList(tJcExpertInfo);
	}
	
    /**
     * 新增专家
     * 
     * @param tJcExpertInfo 专家信息
     * @return 结果
     */
	@Override
	public int insertTJcExpertInfo(TJcExpertInfo tJcExpertInfo)
	{
	    return tJcExpertInfoMapper.insertTJcExpertInfo(tJcExpertInfo);
	}
	
	/**
     * 修改专家
     * 
     * @param tJcExpertInfo 专家信息
     * @return 结果
     */
	@Override
	public int updateTJcExpertInfo(TJcExpertInfo tJcExpertInfo)
	{
	    return tJcExpertInfoMapper.updateTJcExpertInfo(tJcExpertInfo);
	}

	/**
     * 删除专家对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTJcExpertInfoByIds(String ids)
	{
		return tJcExpertInfoMapper.deleteTJcExpertInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * @author 时晨星 --导入专家数据
	 * @param expertList 用户数据列表
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 * @param operName 操作用户
	 * @return 结果
	 */
	@Override
	public String importData(List<TJcExpertInfo> expertList, Boolean isUpdateSupport, String operName)
	{
		if (StringUtils.isNull(expertList) || expertList.size() == 0)
		{
			throw new BusinessException("导入检测机构数据不能为空！");

		}

		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (TJcExpertInfo expert : expertList)
		{
			try
			{
				// 验证是否存在这个检测机构和身份号码
				List<TJcExpertInfo> expertFlag = tJcExpertInfoMapper.getExpertByStatAndName(expert.getStation(),expert.getExpertName());
				if (expertFlag.size()==0)
				{
					expert.setCreateBy(operName);
					this.insertTJcExpertInfo(expert);
					successNum++;
					successMsg.append("<br/>" + successNum + "、专家 " + expert.getExpertName() + " 导入成功");
				}
				else if (isUpdateSupport)
				{
					expert.setUpdateBy(operName);
					expert.setExpertInfoId(expertFlag.get(0).getExpertInfoId());
					this.updateTJcExpertInfo(expert);
					successNum++;
					successMsg.append("<br/>" + successNum + "、专家 " + expert.getExpertName() + " 更新成功");
				}
				else
				{
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、专家 " + expert.getExpertName() + " 已存在");
				}
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、专家 " + expert.getExpertName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new BusinessException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
}
